package com.css.bjdt_core.report.service;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.report.entity.RptTemplateNameOldNew;
import com.css.bjdt_core.report.entity.RptTemplateValidation;
import com.css.bjdt_core.report.entity.ViewRptGroupTemplate;

@Service
public class RptTemplateNameOldNewService {
	@Autowired
	private Dao dao;

	public String getARptTemplateNameOldNew(String templateOldName, String annualId) {
		ViewRptGroupTemplate rptTemplate = dao.fetch(ViewRptGroupTemplate.class,
				Cnd.where("TEMPLATENAME", "like", "%" + templateOldName + "%").and("ANNUALID", "=", annualId));
		if (null == rptTemplate) {
			return dao.fetch(RptTemplateNameOldNew.class,
					Cnd.where("ANNUALID", "=", annualId).and("TEMPLATE_OLD_NAME", "like", "%" + templateOldName + "%"))
					.getTemplateNewName();
		} else {
			return rptTemplate.getTemplateName();
		}
	}

	public void updateValidationInfo(String templateId, String annualId) {
		List<RptTemplateValidation> validationInfoList = dao.query(RptTemplateValidation.class,
				Cnd.where("TEMPLATE_ID", "=", templateId).and("VALIDATION_TYPE", "=", "4"));
		String strSql = "update RPT_TEMPLATE_VALIDATION set VALIDATION_FORMULA_REL = @validationFormulaRel "
				+ " where VALIDATION_ID = @validationId";
		Sql sql = Sqls.create(strSql);
		for (RptTemplateValidation rptTemplateValidation : validationInfoList) {
			String validationFormulaShow = rptTemplateValidation.getValidationFormulaShow();
			//第一步先将公式拆成左右两个部分
			String validationFormula = rptTemplateValidation.getValidationFormulaShow();
			String[] validationNum = validationFormula.split("[\\=\\>\\<\\=>\\=</]");
			//第一步先将公式拆成左右两个部分
			String leftValidationFormula = validationNum[0];
			String rightValidationFormula = validationNum[1];
			try {
				if (rightValidationFormula.length() == 0 || rightValidationFormula.length() == 1) {
					rightValidationFormula = validationNum[2];
				} else {
					rightValidationFormula = validationNum[1];
				}
			} catch (Exception e) {
				System.out.println("------------------>" + validationFormula);
			}
			//左边不用拆
			//再拆右边
			String[] rightValidationFormulaArray = rightValidationFormula.split("[\\+\\-\\*\\=\\>\\</]");
			for (String cellString : rightValidationFormulaArray) {
				String[] cellStringArray = cellString.split("[\\./]");
				String templateName = cellStringArray[0].replaceAll(" ", "");
				ViewRptGroupTemplate viewRptGroupTemplate = dao.fetch(ViewRptGroupTemplate.class,
						Cnd.where("TEMPLATENAME", "like", "%" + templateName + "%").and("ANNUALID", "=", annualId));
				if (null != viewRptGroupTemplate) {
					validationFormulaShow = validationFormulaShow.replaceAll(cellStringArray[0],
							viewRptGroupTemplate.getTemplateId());
				}
			}
			sql.params().set("validationFormulaRel", validationFormulaShow).set("validationId",
					rptTemplateValidation.getValidationId());
			dao.execute(sql);
		}
	}
}
